<template>
  <div class="content">
    <p>合同项目</p>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
      :show-summary="true"
    >
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
  import {
    getListProjectByHome
  } from '@/api/contractproject'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'
  import { formatDate } from '@/utils/index'

  const filePath = {
    props: ['colConfig'],
    methods: {
      openVisible(label, src) {
        const msg = {
          label: label,
          src: src
        }
        this.$emit('openImgVisible', msg)
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        <div>
         <a v-if="scope.row[colConfig.prop]" target="_blank" :href="GLOBAL.filePathUrl+ encodeURI(scope.row[colConfig.prop])">下载</a>
        </div>
      </span>
  </el-table-column>
  `
  }

  const signDate = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const configs = {
    props: ['colConfig'],
    data() {
      return {
        projectId: '',
        infoId: '',
        prop: '',
        contractCode: '',
        projectName: '',
        signDate: '',
        precent: 0,
        progress: ''
      }
    },
    methods: {
      downloadFile(row, prop, column) {
        this.row = row
        this.column = column
        this.projectId = row.projectId
        this.infoId = row.infoId
        this.prop = prop
        this.contractCode = row.contractCode
        this.projectName = row.projectName
        this.signDate = row.signDate
        this.$refs.file.click()
      },
      upLoad(upLoad, params, callback1, callback2) {
        upLoad(params, function(progressEvent) { // 原生获取上传进度的事件
          if (progressEvent.lengthComputable) {
            // 属性lengthComputable主要表明总共需要完成的工作量和已经完成的工作是否可以被测量
            // 如果lengthComputable为false，就获取不到progressEvent.total和progressEvent.loaded
            callback1(progressEvent)
          }
        }).then(res => {
          callback2(res.data)
        })
      },
      fileChanged() {
        const _this = this
        _this.progress = `${_this.row.projectId}${_this.column.id}`
        _this.precent = 0
        const params = new FormData()
        params.append('projectId', this.projectId)
        if (this.prop === 'file') {
          params.append('contractCode', this.contractCode)
          params.append('projectName', this.projectName)
          if (this.signDate) {
            params.append('signDate', this.signDate)
          }
          params.append('file', this.$refs.file.files[0])
          _this.upLoad(updateProject, params, (res) => {
            const loaded = res.loaded
            const total = res.total
            _this.$nextTick(() => {
              _this.precent = (loaded / total) * 100
            })
          }, (res) => {
            _this.$refs.file.value = ''
            _this.progress = ''
            _this.$emit('fetchData')
          })
        } else if (this.prop === 'letterFile') {
          params.append('contractCode', this.contractCode)
          params.append('projectName', this.projectName)
          if (this.signDate) {
            params.append('signDate', this.signDate)
          }
          params.append('letterFile', this.$refs.file.files[0])
          _this.upLoad(updateProject, params, (res) => {
            const loaded = res.loaded
            const total = res.total
            _this.$nextTick(() => {
              _this.precent = (loaded / total) * 100
            })
          }, (res) => {
            _this.$refs.file.value = ''
            _this.progress = ''
            _this.$emit('fetchData')
          })
        } else {
          params.append('infoId', this.infoId)
          params.append(`${this.prop}File`, this.$refs.file.files[0])
          _this.upLoad(updateProjectInfo, params, (res) => {
            const loaded = res.loaded
            const total = res.total
            _this.$nextTick(() => {
              _this.precent = (loaded / total) * 100
            })
          }, (res) => {
            _this.$refs.file.value = ''
            _this.progress = ''
            _this.$emit('fetchData')
          })
        }
      }
    },
    template: `
  <el-table-column :label="colConfig.label">
            <input class="file" type="file" @change="fileChanged" ref="file"/>
            <el-table-column  v-for="(item, index) in colConfig.children"  :label="item.label" :width="item.width">
                <span slot-scope="scope" >
                    <span v-if="item.component==='filePath'">
                        <div v-if="scope.row[item.prop]">
                            <a @click="downloadFile(scope.row,item.prop,scope.column)">上传</a>
                            <a target="_blank" :href="GLOBAL.filePathUrl+ encodeURI(scope.row[item.prop])">下载</a>
                            <el-progress :text-inside="true" :stroke-width="15" v-if="progress===(scope.row.projectId+scope.column.id)" :percentage="precent" status="success"></el-progress>
                        </div>
                        <div v-else>
                            <a style="color: red" >X</a>
                            <a @click="downloadFile(scope.row,item.prop,scope.column)">上传</a>
                            <el-progress :text-inside="true" :stroke-width="15" v-if="progress===(scope.row.projectId+scope.column.id)" :percentage="precent" status="success"></el-progress>
                        </div>
                    </span>
                    <span v-else-if="item.component==='tenderFile'">
                        <div v-if="scope.row[item.prop]">
                            <a @click="downloadFile(scope.row,'letterFile',scope.column)">上传</a>
                            <a target="_blank" :href="GLOBAL.filePathUrl+ encodeURI(scope.row[item.prop])">下载</a>
                            <el-progress :text-inside="true" :stroke-width="15" v-if="progress===(scope.row.projectId+scope.column.id)" :percentage="precent" status="success"></el-progress>
                        </div>
                        <div v-else>
                            <a style="color: red" >X</a>
                            <a @click="downloadFile(scope.row,'letterFile',scope.column)">上传</a>
                            <el-progress :text-inside="true" :stroke-width="15" v-if="progress===(scope.row.projectId+scope.column.id)" :percentage="precent" status="success"></el-progress>
                        </div>
                    </span>
                    <span v-else-if="item.component==='contractFile'">
                        <div v-if="scope.row[item.prop]">
                            <a @click="downloadFile(scope.row,'file',scope.column)">上传</a>
                           <a target="_blank" :href="GLOBAL.filePathUrl+ encodeURI(scope.row[item.prop])">下载</a>
                           <el-progress :text-inside="true" :stroke-width="15" v-if="progress===(scope.row.projectId+scope.column.id)" :percentage="precent" status="success"></el-progress>
                        </div>
                        <div v-else>
                            <a style="color: red" >X</a>
                            <a @click="downloadFile(scope.row,'file',scope.column)">上传</a>
                            <el-progress :text-inside="true" :stroke-width="15" v-if="progress===(scope.row.projectId+scope.column.id)" :percentage="precent" status="success"></el-progress>
                        </div>
                    </span>
                </span>
            </el-table-column>
  </el-table-column>
  `
  }

  const professions = function(row, column) {
    if (row.professions) {
      return row.professions.split('-')[0]
    }
  }

  const isProject = function(row, column) {
    return row.isProject === 1 ? '有' : '无'
  }

  const isAdd = function(row, column) {
    return row.isAdd === 1 ? '增' : '减'
  }

  const projectStage = function(row, column) {
    if (row.projectStage) {
      let obj = {}
      obj = global.projectStageList.find((item) => {
        return item.value === row.projectStage // 筛选出匹配数据
      })
      return obj.label
    }
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    props: ['personneltransferParams'],
    data() {
      return {
        colConfigs: [
          { prop: 'projectName', label: '项目名称', width: '230', fixed: true },
          { prop: 'contractCode', label: '合同编号', width: '120' },
          { prop: 'professions', label: '所属专业', width: '160', formatter: professions },
          { prop: 'teamName', label: '所属团队', width: '100' },
          { prop: 'contractAmount', label: '合同金额(万元)', width: '120' },
          { prop: 'balancePayment', label: '剩余余额(万元)', width: '120' },
          { prop: 'partyA', label: '甲方单位', width: '150' },
          { prop: 'address', label: '项目所属地', width: '150' },
          { prop: 'signDate', label: '签订日期', width: '100', component: signDate },
          { prop: 'projectLeader', label: '项目负责人', width: '100' },
          { prop: 'projectScale', label: '项目规模', width: '80' },
          { prop: 'projectCost', label: '造价(万元)', width: '80' },
          { prop: 'projectStage', label: '项目阶段', width: '80', formatter: projectStage },
          { prop: 'isProject', label: '有无项目子项目', width: '110', formatter: isProject },
          { prop: 'balance', label: '结算差额(万元)', width: '110' },
          { prop: 'isAdd', label: '形式', width: '50', formatter: isAdd }
        ],
        listLoading: true,
        dialogType: 0,
        tableData: [],
        searchFormList: {},
        pageNum: 1,
        pageSize: 10,
        total: 1,
        pca: pca,
        rules: {},
        visible: false
      }
    },
    created() {
    },
    computed: {
      global() {
        return global
      },
      teamId() {
        return this.$store.state.user.teamId
      },
      roleType() {
        return this.$store.state.user.roleType
      },
      teamIds() {
        return this.$store.state.user.teamIds
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.searchFormList.roleType = this.personneltransferParams.roleType
        this.searchFormList.type = this.personneltransferParams.type
        this.searchFormList.date = this.personneltransferParams.date
        this.searchFormList.teamId = this.personneltransferParams.teamId
        this.searchFormList.teamIds = this.teamIds
        this.listLoading = true
        getListProjectByHome(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      }
    },
    watch: {
      'personneltransferParams': {
        handler(newValue, oldValue) {
          this.fetchData()
        },
        immediate: true
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .content {
    padding: 20px;
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
